package cn.kgc.contact.controller;

import cn.kgc.contact.pojo.Contact;
import cn.kgc.contact.pojo.ResultEntity;
import cn.kgc.contact.service.ContactService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ContactController {
    @Resource
    ContactService contactService;

    @RequestMapping("/selectContact")
    public ResultEntity<PageInfo<Contact>> selectContact(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "cname",defaultValue = "") String  cname){
        PageInfo<Contact> pageInfo = contactService.selectAll(pageNum, cname);
        return ResultEntity.successWithoutData(pageInfo);
    }


    //添加信息
    //和修改
    @RequestMapping("/saveContact")
    public ResultEntity<String>  saveContact(@RequestBody Contact contact){
        try{
            Integer saveContact=0;
            if (contact.getId()==null){
                saveContact=contactService.saveContact(contact);
            }else{
                saveContact=contactService.updateContact(contact);
            }
            return ResultEntity.successWithoutData(saveContact+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }


    }

    //根据id查询信息
    @RequestMapping("/getContactById")
    public ResultEntity<Contact> getContactById(Integer id){
        Contact selectById = contactService.selectById(id);
        return ResultEntity.successWithoutData(selectById);
    }

}
